
import UIKit

class CCMainViewContorller: UITabBarController {

    //撰写按钮点击时间
    func composeClick(){
        print(__FUNCTION__)
    }
    override func viewDidLoad(){
        super.viewDidLoad()
        //设置自定义的tabBar
        let newTabbar = CCTabbar()
        //KVC
        setValue(newTabbar, forKey: "tabBar")
        tabBar.tintColor = UIColor.orangeColor()
        
        //第一种适合图片和文字颜色一样的时候
        tabBar.tintColor = UIColor.orangeColor()
        
    //首页
    let homeVC = CCHomeTabarViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
    //消息
    let messageVC = CCMessageTabarViewController()
        addChildViewController( messageVC, title:"消息", imageName: "tabbar_message_center")
        //占位
        addChildViewController(UIViewController())
        
        
    //发现
    let discoerVC = CCDiscoerTabbarViewController()
        addChildViewController(discoerVC, title: "发现", imageName:"tabbar_discover" )
        
    //我的资料
    let profileVC = CCProfileTabbarViewController()
    addChildViewController(profileVC, title: "我的信息", imageName:"tabbar_profile")
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width, y: 0, width: width+10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }
    
    

    //抽取出来的方法，控制器和图片名称和导航栏需要外界传过来，设置成参数
    //把方法私有，只有本类和当前文件的其他类可以使用
    private func addChildViewController(controller: UIViewController,title:String,imageName:String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        //拼接高亮名称
        let highlightedImageName = imageName + "_highlighted"
        //让选择的图片使用原来的颜色，不要系统帮我们渲染
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
//        //设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    //        // 懒加载
    //        /// 撰写按钮
            lazy var composeButton: UIButton = {
                let button = UIButton()
            // 设置参数
                // 设置按钮背景图片
                button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
                button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
                // 设置按钮图片
                button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
                button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
                // 添加到tabbar里面
                self.tabBar.addSubview(button)
                //添加点击事件
                button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
                return button
                }()
}
